package com.qf.student.utils;

import java.sql.Connection;
import java.sql.SQLException;

public class TxManager {

    public static void begin() {
        Connection connection = DBMangaerUtils.getConnection();
        try {
            connection.setAutoCommit(false);
            ConnectionThreadLocal.set(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void commit() {
        try {
            Connection connection = ConnectionThreadLocal.get();
            if (connection != null) {
                connection.commit();
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    public static void rollback() {
        try {
            Connection connection = ConnectionThreadLocal.get();
            if (connection != null) {
                connection.rollback();
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
